package com.shwread.android.qysw.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.GlideCircleTransform;
import com.bumptech.glide.Glide;
import com.ciko.code.logic.bean.AddressBookInfo;
import com.ciko.code.logic.bean.ContactBookInfo;
import com.google.gson.Gson;
import com.netease.nim.demo.session.activity.P2PMessageActivity;
import com.netease.nim.demo.session.activity.TeamMessageActivity;
import com.netease.nimlib.sdk.team.model.Team;
import com.shwread.android.qysw10000227.R;
import com.shwread.android.ui.dialog.ContactCallSelectDialog;

import java.util.ArrayList;
import java.util.List;

import logic.bean.ContactInfo;
import logic.constants.Const;
import logic.util.Util;
import logic.util.Utils;


public class ChatPersonalInfoActivity
		extends
		BaseBusinessActivity
		implements
		OnClickListener
{
	private Context                 context;

	private TextView                tvTitle, tvName, tvPost, tvMobilePhone, tvPhone, tvEmployer, tvAddress, tvCode,tvEmail;
	private Button                  btnBack, btnChat, btnMsg, btnCall;
	private ImageView               ivHeadHeaderImg,img_sex;
//	private CircleImageView ivHeadImg;
	private ImageView ivHeadImg;
	private LinearLayout            llEmployer, llAddress, llCode,llTelephone,llEmail;

	private String                  groupName;
	private ContactBookInfo         contactInfo;
	private AddressBookInfo addressBookInfo;

	private ContactCallSelectDialog selectNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_personal_information);
		context = this;
		Intent intent = getIntent();
		groupName = intent.getStringExtra("groupName");
		if (intent.getSerializableExtra("contactInfo") instanceof ContactBookInfo){
			contactInfo = (ContactBookInfo)intent.getSerializableExtra("contactInfo");
		}else{
			addressBookInfo = (AddressBookInfo)intent.getSerializableExtra("contactInfo");
			Gson gson = new Gson();
			String beanJson = gson.toJson(addressBookInfo);
			contactInfo = gson.fromJson(beanJson,ContactBookInfo.class);
		}
		initView();
		setListeners();
	}

	private void setListeners() {
		btnBack.setOnClickListener(this);
		btnChat.setOnClickListener(this);
		btnMsg.setOnClickListener(this);
		btnCall.setOnClickListener(this);
	}

	private void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("个人信息");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		tvName = (TextView) findViewById(R.id.tv_chat_name);
		tvPost = (TextView) findViewById(R.id.tv_chat_post);
//		ivHeadImg = (CircleImageView) findViewById(R.id.iv_chat_head_img);
		ivHeadImg = (ImageView) findViewById(R.id.iv_chat_head_img);
		ivHeadHeaderImg = (ImageView) findViewById(R.id.iv_chat_head_heder_img);
		img_sex = (ImageView) findViewById(R.id.img_chat_sex);
		tvMobilePhone = (TextView) findViewById(R.id.tv_chat_phone);
		tvPhone = (TextView) findViewById(R.id.tv_chat_telephone);
		llTelephone = (LinearLayout) findViewById(R.id.ll_chat_telephone);
		llEmail = (LinearLayout) findViewById(R.id.ll_chat_email);
		tvEmail = (TextView) findViewById(R.id.tv_chat_email);
		llEmployer = (LinearLayout) findViewById(R.id.ll_chat_employer);
		tvEmployer = (TextView) findViewById(R.id.tv_chat_employer);
		llAddress = (LinearLayout) findViewById(R.id.ll_chat_address);
		tvAddress = (TextView) findViewById(R.id.tv_chat_address);
		llCode = (LinearLayout) findViewById(R.id.ll_chat_code);
		tvCode = (TextView) findViewById(R.id.tv_chat_code);
		btnChat = (Button) findViewById(R.id.btn_chat_chat);
		btnMsg = (Button) findViewById(R.id.btn_chat_send_msg);
		btnCall = (Button) findViewById(R.id.btn_chat_call);

		tvName.setText(contactInfo.getContactName());
		tvPost.setText(groupName);
		if (Utils.isNotEmpty(contactInfo.getMobileNumber()))
		{
			tvMobilePhone.setText(contactInfo.getMobileNumber());
		}
		else
		{
			tvMobilePhone.setVisibility(View.GONE);
			btnChat.setVisibility(View.GONE);
			btnMsg.setVisibility(View.GONE);
		}
		if (Utils.isNotEmpty(contactInfo.getPhoneNumber()))
		{
			tvPhone.setText(contactInfo.getPhoneNumber());
		}
		else
		{
//			tvPhone.setVisibility(View.GONE);
			llTelephone.setVisibility(View.GONE);
		}
		if (Utils.isNotEmpty(contactInfo.getEmail())) {//邮箱
			tvEmail.setText(contactInfo.getEmail());
		} else {
			llEmail.setVisibility(View.GONE);
		}
		switch (contactInfo.getSex()) {//设置性别
			case 1:
				img_sex.setImageResource(R.drawable.img_chat_man);
				break;
			case 2:
				img_sex.setImageResource(R.drawable.img_chat_lady);
				break;
			default:
				img_sex.setVisibility(View.GONE);
				break;
		}
		if (Utils.isNotEmpty(contactInfo.getOrganization()) || Utils.isNotEmpty(contactInfo.getPosition())) {
			String employer = Util.NUllToString(contactInfo.getOrganization())
					+ Util.NUllToString(contactInfo.getPosition());
			tvEmployer.setText(employer);
		} else {
			llEmployer.setVisibility(View.GONE);
		}
		if (Utils.isNotEmpty(contactInfo.getAddress()))
		{
			tvAddress.setText(contactInfo.getAddress());
		}
		else
		{
			llAddress.setVisibility(View.GONE);
		}
		if (Utils.isNotEmpty(contactInfo.getPostCode()))
		{
			tvCode.setText(contactInfo.getPostCode());
		}
		else
		{
			llCode.setVisibility(View.GONE);
		}
		if (Utils.isNotEmpty(contactInfo.getUserHead()))
		{
			Glide.with(context).load(contactInfo.getUserHead()).transform(new GlideCircleTransform(context)).into(ivHeadImg);
		}
		if (contactInfo.getGroupPosition() == 1)
		{
			Glide.with(context).load(R.drawable.chat_contract_icon_header).into(ivHeadHeaderImg);
			ivHeadHeaderImg.setVisibility(View.VISIBLE);
		}
		else if (contactInfo.getGroupPosition() == 2)
		{
			Glide.with(context).load(R.drawable.chat_contract_icon_deputy_header).into(ivHeadHeaderImg);
			ivHeadHeaderImg.setVisibility(View.VISIBLE);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				break;
			case R.id.btn_chat_chat:
				if (contactInfo.getUserId().equals(Const.UserStaffId))
				{
					Util.showToast(context, "您不能与自己进行聊天");
					return;
				}
				ContactInfo info = new ContactInfo();
				info.setAccId(contactInfo.getAccId());
				info.setAccount(contactInfo.getMobileNumber());
				info.setHeadImage(contactInfo.getUserHead());
				info.setStaffName(contactInfo.getContactName());
				talkto(info);
				break;
			case R.id.btn_chat_send_msg:
				if (Utils.isNotEmpty(contactInfo.getMobileNumber()))
				{
					doSendSMSTo(contactInfo.getMobileNumber(), null);
				}
				break;
			case R.id.btn_chat_call:
				List<String> phoneList = new ArrayList<String>();
				if (Utils.isNotEmpty(contactInfo.getMobileNumber()))
				{
					phoneList.add(contactInfo.getMobileNumber());
				}
				if (Utils.isNotEmpty(contactInfo.getPhoneNumber()))
				{
					phoneList.add(contactInfo.getPhoneNumber());
				}
				selectNum = new ContactCallSelectDialog(context, phoneList);
				if (phoneList.size() > 1)
				{
					selectNum.show();
				}
				else
				{
					dialPhoneNumber(phoneList.get(0));
				}
				//				if (Utils.isNotEmpty(contactInfo.getMobileNumber()))
				//				{
				//					dialPhoneNumber(contactInfo.getMobileNumber());
				//				}
				//				else if (Utils.isNotEmpty(contactInfo.getPhoneNumber()))
				//				{
				//					dialPhoneNumber(contactInfo.getPhoneNumber());
				//				}
				break;
			default:
				break;
		}
	}

	/**
	 * 跳转到聊天页面
	 *
	 * @param item
	 */
	protected void talkto(Object item) {
		if (item instanceof ContactInfo)
		{
			ContactInfo info = (ContactInfo) item;
			String str;
			if (Utils.isNotEmpty(info.getStaffName()))
			{
				str = info.getStaffName();
			}
			else if (Utils.isNotEmpty(info.getNickName()))
			{
				str = info.getNickName();
			}
			else
			{
				str = info.getAccount();
			}
			//			Intent intent = new Intent();
			//			intent.putExtra(DefaultConsts.TO_P2P_PAGE_TAG, DefaultConsts.TO_P2P_PAGE_FROM_CONTACT_MANGER);
			//			intent.putExtra(DefaultConsts.TO_P2P_PAGE_INFO, str);
			//			P2PMessageActivity.start(getActivity(), info.getAccId(), intent);
			P2PMessageActivity.start(context, info.getAccId(), str);
			//			startActivity(new Intent(getActivity(), P2PMessageActivity.class));
		}
		else if (item instanceof Team)
		{
			Team team = (Team) item;
			TeamMessageActivity.start(context, team.getId());
		}

	}

	/**
	 * 调起系统发短信功能
	 *
	 * @param phoneNumber
	 * @param message
	 */
	public void doSendSMSTo(String phoneNumber, String message) {
		if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber))
		{
			Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
			intent.putExtra("sms_body", message);
			context.startActivity(intent);
		}
	}

	/**
	 * 调起系统拨号盘
	 *
	 * @param phoneNumber
	 */
	public void dialPhoneNumber(String phoneNumber) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		context.startActivity(intent);
	}

	@Override
	public Handler getUIHandler() {
		return null;
	}

}
